package com.lemon.smartsafetynew.ui.fragment.contact;

import android.os.Bundle;

import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.app.RefreshType;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafetynew.ui.fragment.base.BasePullToRefreshFragment;
import com.lemon.smartsafety.utils.app.HUDUtil;
import com.lemon.smartsafetynew.entity.User;
import com.lemon.smartsafetynew.entity.contact.DepartmentVo;
import com.lemon.smartsafetynew.entity.event.common.ViewEvent;
import com.lemon.smartsafetynew.entity.event.contact.DepartmentEvent;
import com.lemon.smartsafetynew.net.services.ContactServices;
import com.lemon.smartsafetynew.ui.adapter.contact.DepartmentListAdapter;
import com.lemon.smartsafetynew.util.constants.KeyConstants;
import com.lemon.smartsafetynew.util.user.AccountUtil;

import java.util.List;

import kale.adapter.LemonRcvAdapter;

/**
 * Created by Andrew on 2017/9/11.
 */

public class DepartmentListFragment extends BasePullToRefreshFragment<DepartmentVo, List<DepartmentVo>>
        implements RecyclerTClickListener<DepartmentVo> {

    private User user;

    private static String tag = "";

    @Override
    protected void getBaseArguments() {
        super.getBaseArguments();

        Bundle bundle = getArguments();

        if (bundle != null) {
            if(bundle.containsKey(KeyConstants.KEY_TAG)) {
                tag = bundle.getString(KeyConstants.KEY_TAG);
            }
        }
    }

    public static DepartmentListFragment newInstance(String tag) {

        Bundle args = new Bundle();
        args.putString(KeyConstants.KEY_TAG, tag);

        DepartmentListFragment fragment = new DepartmentListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onPullRefresh() {

        LemonRequestParams params = new LemonRequestParams("");
        params.put("company_id", user.getCompanyId());
        params.put("page_size", pageSize);
        params.put("current_page", currentPage);

        ContactServices
                .init()
                .getDepartment(RefreshType.REFRESH, params);
    }

    @Override
    public void onLoadMore() {

        LemonRequestParams params = new LemonRequestParams("");
        params.put("company_id", user.getCompanyId());
        params.put("page_size", pageSize);
        params.put("current_page", currentPage);

        ContactServices
                .init()
                .getDepartment(RefreshType.LOAD_MORE, params);

    }

    @Override
    public boolean goRefreshOnCreate() {
        return true;
    }

    @Override
    public LemonRcvAdapter<DepartmentVo> getLemonAdapter() {
        return new DepartmentListAdapter(mDataList, this);
    }

    @Override
    public String getHttpTag() {
        return KeyConstants.URL_GET_COMPANY_DEPARTMENT;
    }

    @Override
    public void onItemClick(DepartmentVo data, int position) {

        DepartmentEvent departmentEvent = new DepartmentEvent();
        departmentEvent.setDepartmentVo(data);
        departmentEvent.setTag(tag);
        AppService.getBus().post(departmentEvent);

        AppService.getBus().post(new ViewEvent(DepartmentListFragment.class.getName()));
    }

    @Override
    protected void initViews() {
        super.initViews();

        initData();
    }


    /**
     * 初始化数据变量
     */
    private void initData() {

        user = AccountUtil.getUser();

        if (user == null) {
            mHUDUtil.showErrorTip("用户信息为空，请重新登陆！", new HUDUtil.CallBack() {
                @Override
                public void onCall() {

                }
            });
        }
    }
}
